import UtilFuncs from '../common/tools/utilfuncs.js'
var funcs = UtilFuncs.UtilFuncs;

import GlobalConst from '../common/globalConst.js'
var global = GlobalConst.GlobalConst;
import LGXCOM from '../common/common.js'

import {LGXEnums}  from '../common/sysenum.js';




var LineTangent2CircleSvc = {}

LineTangent2CircleSvc.snapLine2Circle = function (pCurentGraph,pDataMng, mousePoint) {
    if (!pCurentGraph){
        return null;
    }
    else if (pCurentGraph.m_graphType != LGXEnums.LGXGraphType.LineGraph_type){
        return null;
    }
    let translateFlag = true;
    if (pCurentGraph) {
        translateFlag = pCurentGraph.chkInTranslating();
    }
    let rst = this.snapMousePos2SurroundCircleGraph(pCurentGraph,pDataMng, mousePoint, translateFlag);
    return rst;
}

LineTangent2CircleSvc.snapMousePos2SurroundCircleGraph = function (pCurentGraph,pDataMng, mousePoint, translateFlag) {
    let testPt = {x:467,y:449};
    let testdis = funcs.distancePoint(mousePoint,testPt,1);
    if (testdis < 5){
        console.log();
    }

    let absorbFlag = false;
    let abortPt = null;
    var result = this.chkSurroundCircles(pCurentGraph,pDataMng, mousePoint, translateFlag);
    let auxDataMng = pDataMng.getAuxDataManager();
    if (result.pt) {
        abortPt = result.pt;
        auxDataMng.setAbsorbPointPos(mousePoint);
        auxDataMng.setAbsorbPointDis(1);
        absorbFlag = true;
        auxDataMng.setAbsorbPointKind(LGXEnums.AuxPointKind.tangentPt);
    }
    else {
        auxDataMng.setAbsorbPointKind(LGXEnums.AuxPointKind.none);
        auxDataMng.setAbsorbPointDis(0);
    }
    let ptInfo = null;
    if (abortPt){
        ptInfo = {pt:abortPt,kind:LGXEnums.AuxPointKind.tangentPt};
    }
    return {ptInfo,flag:absorbFlag};
}


LineTangent2CircleSvc.chkSurroundCircles = function (pCurentGraph,pDataMng, mousePoint, translateFlag) {
    let scaling = pDataMng.getScaling();
    var minDis = -1;
    let abortPoint = null;

    let size = global.SelectGap / scaling;
    let mouseRect = new LGXCOM.LGXRect();
    mouseRect.left = mousePoint.x - size;
    mouseRect.right = mousePoint.x + size;
    mouseRect.bottom = mousePoint.y - size;
    mouseRect.top = mousePoint.y + size;

    var graphLst = pDataMng.getAllGraphs();
    var count = graphLst.length;

    //先检测尝试对齐主特征点
    for (let i = 0; i < count; i++) {
        let pGraph = graphLst[i];
        if (pGraph.m_graphType != LGXEnums.LGXGraphType.CircleGraph_type
            && pGraph.m_graphType != LGXEnums.LGXGraphType.ArcGraph_type){
                continue;
            }
        if (!this.chkCanAbsorb(pCurentGraph, pGraph)) {
            continue;
        }

        if (!funcs.chkOverlap(mouseRect, pGraph.getZone())) {
            continue;
        }

        var interPointLst = this.getLineInterCirclePoints(pCurentGraph,pGraph);
        if (!interPointLst) {
            continue;
        }

        if (interPointLst.length == 1){
            let pt = interPointLst[0];
            abortPoint = pt;
            minDis = 0;
            break;
        }
        else if (interPointLst.length == 2){
            let interInfo1 = interPointLst[0];
            let interInfo2 = interPointLst[1];
            let dis = funcs.distancePoint(interInfo1,interInfo2,scaling);
            if (dis < global.SelectGap/4){
                abortPoint = this.getTangentPt(pCurentGraph.getPosCopy(),pGraph.getPosCopy());
                minDis = 0;
                break;
            }
        }
    }
    var rst = { pt: abortPoint, dis: minDis };
    return rst;
}


LineTangent2CircleSvc.getTangentPt = function(linePos,circlePos){
    let linePtCount = linePos.posList.length;
    if (linePtCount < 2){
        return null;
    }
    let linePrePt = linePos.posList[linePtCount - 2];
    let lineCurentPt = linePos.posList[linePtCount - 1];

    let radius = circlePos.radius;

    let disPt2Cnt = funcs.distancePoint(circlePos,linePrePt,1);
    let disPrePt2TangentPt = Math.sqrt(disPt2Cnt*disPt2Cnt - radius*radius);
    let sinVal = radius/disPt2Cnt;
    let angleRad = Math.asin(sinVal);
    let angle = funcs.radian2Degree(angleRad);
    let vect = funcs.getVector(linePrePt,circlePos);
    let vect1 = vect.rotateCopy(angle);
    let x = linePrePt.x + vect1.m*disPrePt2TangentPt;
    let y = linePrePt.y + vect1.n*disPrePt2TangentPt;
    let p1 = {x,y};
    let dis1 = funcs.distancePoint(lineCurentPt,p1,1);

    let vect2 = vect.rotateCopy(-angle);
    x = linePrePt.x + vect2.m*disPrePt2TangentPt;
    y = linePrePt.y + vect2.n*disPrePt2TangentPt;
    let p2 = {x,y};
    let dis2 = funcs.distancePoint(lineCurentPt,p2,1);
    if (dis1 < dis2){
        return p1;
    }
    else{
        return p2;
    }
}





LineTangent2CircleSvc.getLineInterCirclePoints = function (lineGraph,circleGraph) {
    let rstArr = [];
    let linePos = lineGraph.getPosCopy();
    let linePtCount = linePos.posList.length;
    if (linePtCount < 2){
        return rstArr;
    }
    let p1 = linePos.posList[linePtCount - 2];
    let p2 = linePos.posList[linePtCount - 1];
    rstArr = funcs.getLineInterCircle(p1,p2,circleGraph.getPosCopy());
    return rstArr;
}

LineTangent2CircleSvc.chkCanAbsorb = function (pCurentGraph, pDstGraph) {
    if (pCurentGraph == pDstGraph
        || pDstGraph.getFinish() == 0
        || pDstGraph.getUseful() != 1
        || pDstGraph.getModifing() == 1
        || pDstGraph.m_graphType == LGXEnums.LGXGraphType.Combine_type
        || pDstGraph.m_graphType == LGXEnums.LGXGraphType.TextGraph_type
        || pDstGraph.m_graphType == LGXEnums.LGXGraphType.DimTextGraph_type
        || pDstGraph.m_graphType == LGXEnums.LGXGraphType.CADText_type
        || pDstGraph.m_graphType == LGXEnums.LGXGraphType.FastToolPanel_type
        || funcs.group(pDstGraph.getType()) == global.dimGroup
        ) {
        return false;
    }

    let flag = true;
    if (pCurentGraph) {
        flag = false;
        let rect1 = pCurentGraph.getZone();
        let rect2 = pDstGraph.getZone();
        if (funcs.chkOverlap(rect1, rect2)) {
            flag = true;
        }
    }
    return flag;
}

export  { LineTangent2CircleSvc }


